package com.hoow.shakoow.shake.components.dialog

import android.os.Bundle
import android.view.*
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import com.hoow.shakoow.R
import com.hoow.shakoow.base.BaseDialogAction
import com.hoow.shakoow.databinding.AdUnifiedBinding
import com.hoow.shakoow.databinding.DialogPurchaseTimesBinding

class PurchaseTimesDialog : DialogFragment(), BaseDialogAction {

    companion object {
        // 看广告
        const val ACTION_WATCH_ADS = 0

        // 购买
        const val ACTION_PURCHASE = 1

        // 加载广告
        const val ACTION_LOAD_ADS = 2

        // 是否已显示
        var isShowNow = false

        fun showDialog(
            fragmentManager: FragmentManager,
            listener: (Int) -> Unit = {}
        ) {
            if (!isShowNow) {
                isShowNow = true

                val dialog = PurchaseTimesDialog()
                dialog.mClickActionListener = listener
                dialog.showNow(fragmentManager, PurchaseTimesDialog::class.java.simpleName)
            }
        }
    }

    lateinit var mViewBinding: DialogPurchaseTimesBinding

    lateinit var unifiedAdBinding:AdUnifiedBinding


    private var mClickActionListener: (Int) -> Unit = {}

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View {
        dialog?.requestWindowFeature(Window.FEATURE_NO_TITLE);
        mViewBinding =
            DialogPurchaseTimesBinding.inflate(layoutInflater)

        unifiedAdBinding = AdUnifiedBinding.inflate(layoutInflater)

        return mViewBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initSubviews()
        dialog?.setCancelable(false)
        dialog?.setCanceledOnTouchOutside(false)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_TITLE, R.style.Dialog)
    }

    override fun onStart() {
        super.onStart()

        dialog?.window?.let {
            it.attributes.gravity = Gravity.CENTER
            it.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
//            it.setWindowAnimations(R.style.bottomDialogAnimation)
        }

        mClickActionListener.invoke(ACTION_LOAD_ADS)
    }

    override fun initSubviews() {
        try {
            mViewBinding.textWatchAds.setOnClickListener {
                mClickActionListener.invoke(ACTION_WATCH_ADS)
                isShowNow = false
                dismiss()
            }

            mViewBinding.textNoAds.setOnClickListener {
                mClickActionListener.invoke(ACTION_PURCHASE)
                isShowNow = false
                dismiss()
            }

            mViewBinding.imageDialogPurchaseClose.setOnClickListener {
                isShowNow = false
                dismiss()
            }

//            mViewBinding.refreshButton.setOnClickListener {
//                mClickActionListener.invoke(ACTION_LOAD_ADS)
//            }

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    override fun showWithUITag(
        gTag: String,
        fragmentManager: FragmentManager
    ) {

    }

    override fun setOnActionClickListener(listener: (Int) -> Unit) {
        mClickActionListener = listener
    }

}